package com.briup.handler;

import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 处理程序异常
     *
     * @param exception 例外
     * @return {@link Result}
     */
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception exception){
        log.error("捕获到异常信息");
        //打印异常信息
        log.error(exception.getMessage());
        //判断该异常是否是系统自定义异常
        if (exception instanceof SystemException){
            SystemException systemException = (SystemException) exception;
            //属于系统自定义异常
            return Result.error(systemException.getCodeAndMessageEnum());
        }
        //否则认为是系统未知异常
        return Result.error(CodeAndMessageEnum.ERROR);
    }
}
